package com.way.fileexplorer.util;

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;

import com.way.fileexplorer.R;
import com.way.fileexplorer.activities.FileExploreActivity.NavigationCallback;
import com.way.fileexplorer.activities.FilePasteActivity;
import com.way.fileexplorer.activities.FileSearchResultActivity;
import com.way.fileexplorer.adapters.CopyDestListAdapter;
import com.way.fileexplorer.fragments.DetailedListFragment;

public class ActivityUtils {
	private static ArrayAdapter<String> mNavigationAdapter = null;
	private static ArrayAdapter<String> mSearchLocationAdapter = null;
	private static ArrayList<String> mNavigationList = null;
	private static ArrayList<String> mSearchLocationList = null;
	private static Context mContext = null;

	public static void initAdapters(Context context) {
		mContext = context;
		if (mNavigationList == null) {
			mNavigationList = new ArrayList<String>();
		}
		if (mSearchLocationList == null) {
			mSearchLocationList = new ArrayList<String>();
		}
		if (mNavigationAdapter == null) {
			mNavigationAdapter = new ArrayAdapter<String>(context,
					android.R.layout.simple_spinner_dropdown_item,
					mNavigationList);
		}
		if (mSearchLocationAdapter == null) {
			mSearchLocationAdapter = new ArrayAdapter<String>(context,
					android.R.layout.simple_spinner_dropdown_item,
					mSearchLocationList);
		}
	}

	public static String getSearchLocation(int index) {
		String str = "";
		// bug192671 begin
		if (index >= 0 && mSearchLocationList != null
				&& index < mSearchLocationList.size()) {
			str = mSearchLocationList.get(index);
		}
		// bug192671 end
		return str;
	}

	public static void setNavigationList(ArrayList<String> list) {
		if (list == null) {
			throw new NullPointerException("setNavigationList(null) ?");
		}
		if (mNavigationList != null) {
			mNavigationList.clear();
			if (list.isEmpty()) {
				mNavigationAdapter.notifyDataSetChanged();
				return;
			}
			for (String s : list) {
				mNavigationList.add(s);
			}
			mNavigationAdapter.notifyDataSetChanged();
		}
	}

	public static void setSearchLocation(ArrayList<String> list) {
		if (list == null) {
			throw new NullPointerException("setNavigationList(null) ?");
		}
		if (mSearchLocationList != null) {
			mSearchLocationList.clear();
			if (list.isEmpty()) {
				mSearchLocationAdapter.notifyDataSetChanged();
				return;
			}
			for (String s : list) {
				mSearchLocationList.add(s);
			}
			mSearchLocationAdapter.notifyDataSetChanged();
		}
	}

	public static ArrayAdapter<String> getNavigationAdapter() {
		return mNavigationAdapter;
	}

	public static ArrayAdapter<String> getSearchLocationAdapter() {
		return mSearchLocationAdapter;
	}

	public static ActionBar createActionBar(Context context,
			NavigationCallback callback) {
		ActionBar actionBar = ((Activity) context).getActionBar();
		if (null == actionBar) {
			throw new NullPointerException(
					"Actionbar is null, check your context");
		}

		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE
				| ActionBar.DISPLAY_HOME_AS_UP);
		if (callback != null) {
			actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
			actionBar.setListNavigationCallbacks(mNavigationAdapter, callback);
		}
		actionBar.setDisplayShowHomeEnabled(true);
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setHomeButtonEnabled(true);

		return actionBar;
	}

	public static void showDestSelectDialog(final Context context,
			DialogInterface.OnClickListener listener) {
		if (context != null) {
			Builder builder = new AlertDialog.Builder(context);
			builder.setTitle(R.string.select_destination);
			BaseAdapter adapter = new CopyDestListAdapter();
			builder.setAdapter(adapter, listener);
			builder.create().show();
		}
	}

	public static void startPasteMode(int position) {
		if (mContext != null) {
			boolean bIsSdCard = false;
			if (position > 0) {
				bIsSdCard = true;
			}
			((Activity) mContext).startActivityForResult(
					new Intent().setClass(mContext.getApplicationContext(),
							FilePasteActivity.class).putExtra(
							StorageUtil.SDCARD_TYPE, bIsSdCard),
					DetailedListFragment.PASTE_PATH);
		}
	}

	public static void startSearchMode(Bundle bun, Context context) {
		if (context != null && bun != null) {
			((Activity) context).startActivity(new Intent().setClass(
					context.getApplicationContext(),
					FileSearchResultActivity.class).putExtra(
					FileSearch.SEARCH_ATTACH, bun));
		}
	}

	public static void showSearchLocationDialog(final Context context,
			DialogInterface.OnClickListener listener) {
		if (context != null && mSearchLocationAdapter != null) {
			Builder builder = new AlertDialog.Builder(context);
			builder.setTitle(R.string.fragment_search_location);
			builder.setAdapter(mSearchLocationAdapter, listener);
			builder.create().show();
		}
	}

	public static int getSearchLocationSize() {
		return mSearchLocationAdapter.getCount();
	}
}
